package com.ny.zuoye2567.dao.view界面;

import com.ny.zuoye2567.dao.service.*;
import com.ny.zuoye2567.dao.shiti.Manager;
import com.ny.zuoye2567.dao.shiti.Order;
import com.ny.zuoye2567.dao.shiti.Product;
import com.ny.zuoye2567.dao.shiti.Student;

import java.util.List;
import java.util.Scanner;

public class IndexView {
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService = new ProductServiceImpl();
    private static OrderService orderService = new OrderServiceImpl();
    private static ManagerService managerService = new ManagerServiceImpl();

    public static void main(String[] args) throws Exception {
        boolean flag = true;
        while (flag) {
            System.out.println("欢迎来到校园商品系统");
            System.out.println("请选择");
            System.out.println("1.用户界面");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("4.管理员系统");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            switch (n) {

                case 1:
                    System.out.println("请登录");
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入密码");
                    String password = scanner.next();
                    Student student = studentService.login(username, password);

                    if (student != null) {
                        System.out.println("登陆成功：来到商品展示界面");

                        List<Product> productList = productService.findAllProduct();
                        System.out.print("商品序号" + "\t" + "\t");
                        System.out.print("商品名称" + "\t" + "\t");
                        System.out.print("商品库存" + "\t" + "\t");
                        System.out.print("商品种类" + "\t" + "\t");
                        System.out.println("商品价格" + "\t" + "\t");

                        for (Product product : productList) {
                            System.out.print(product.getProductId() + " \t\t\t");
                            System.out.print(product.getProductName() + " \t\t\t");
                            System.out.print(product.getStock() + " \t\t");
                            System.out.print(product.getCategory() + "\t\t\t");
                            System.out.print(product.getPrice() + " \t\t\t");
                            System.out.println(product.getDescription());
                        }
                        System.out.println("请选择商品序号");
                        int productId = scanner.nextInt();

                        // 检查商品ID是否有效
                        if (productId < 1 || productId > productList.size()) {
                            System.out.println("无效的商品序号，请重新选择。");
                            break;
                        }
                        Product selectedProduct = productList.get(productId - 1);
                        System.out.println("当前库存数量为：" + selectedProduct.getStock());

                        System.out.println("请输入商品数量");
                        int num = scanner.nextInt();
                        if (num > selectedProduct.getStock()) {
                            System.out.println("库存不足！");
                            break;
                        }
                        Order order = new Order();
                        order.setStudentId(student.getStudentId());
                        order.setProductId(productId);
                        order.setNumber(num);
                        order.setPrice(productService.findAllProduct().get(productId - 1).getPrice() * num);
                        int i = orderService.insertOrder(order);
                        if (i > 0) {
                            System.out.println("下单成功");
                        }
                    } else {
                        System.out.println("重新登录");
                    }
                    break;

                case 2:
                    System.out.println("请注册");
                    Student s = new Student();
                    System.out.println("请输入用户名");
                    String name = scanner.next();
                    System.out.println("请输入手机哈");
                    String phone = scanner.next();
                    System.out.println("请输入密码");
                    String pwd = scanner.next();
                    s.setName(name);
                    s.setPhone(phone);
                    s.setPassword(pwd);
                    int i = studentService.insertStudent(s);
                    if (i > 0) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    System.out.println("退出系统");
                    flag = false;
                    break;

                case 4:
                    boolean flag1 = true;
                    while (flag1) {
                        System.out.println("欢迎来到管理员系统");
                        System.out.println("请选择");
                        System.out.println("1.管理用户信息");
                        System.out.println("2.管理商品信息");
                        System.out.println("3.管理订单信息");
                        System.out.println("4.退出管理员系统");

                        int d = scanner.nextInt();
                        switch (d) {
                            case 1:
                                boolean flag2 = true;
                                while (flag2) {
                                    System.out.println("1.管理员用户登录");
                                    System.out.println("2.管理员注册");
                                    System.out.println("3.管理员信息修改");
                                    System.out.println("4.管理员用户删除");
                                    System.out.println("5.退出管理员信息系统");

                                    int a = scanner.nextInt();
                                    switch (a) {
                                        case 1:
                                            System.out.println("请登录");
                                            System.out.println("请输入管理员用户名");
                                            String Managername = scanner.next();
                                            System.out.println("请输入管理员密码");
                                            String Managerpwd = scanner.next();

                                            Manager manager = managerService.login(Managername, Managerpwd);
                                            if (manager != null) {
                                                System.out.println("登录成功：来到管理员信息显示界面");
                                                List<Manager> managerList = managerService.findAllManager();
                                                System.out.print("管理员编号" + "\t" + "\t");
                                                System.out.print("管理员姓名" + "\t" + "\t");
                                                System.out.print("管理员电话" + "\t" + "\t");
                                                System.out.println("管理员密码");

                                                for (Manager manager1 : managerList) {
                                                    System.out.print(manager1.getManagerId() + "\t" + "\t" + " " + " " + " " + " " + " " + " " + " " + " ");
                                                    System.out.print(manager1.getName() + "\t" + "\t" + " " + " " + " ");
                                                    System.out.print(manager1.getPhone() + " " + "\t" + "\t");
                                                    System.out.println(manager1.getPassword() + " " + "\t" + "\t");
                                                }
                                            }
                                            break;
                                        case 2:
                                            System.out.println("请注册");
                                            System.out.println("请输入用户名");
                                            String ManagerName = scanner.next();
                                            System.out.println("请输入手机号");
                                            String ManagerPhone = scanner.next();
                                            System.out.println("请输入密码");
                                            String ManagerPassword = scanner.next();

                                            Manager m = new Manager();
                                            m.setName(ManagerName);
                                            m.setPhone(ManagerPhone);
                                            m.setPassword(ManagerPassword);

                                            int b = managerService.insertManager(m);
                                            if (b > 0) {
                                                System.out.println("注册成功");
                                            } else {
                                                System.out.println("注册失败");
                                            }
                                            break;
                                        case 3:
                                            System.out.println("请输入要修改的用户名");
                                            String oldMname = scanner.next();
                                            System.out.println("请输入需要修改的用户名的密码");
                                            String oldMpwd = scanner.next();

                                            Manager Upm = managerService.login(oldMname, oldMpwd);
                                            if (Upm == null) {
                                                System.out.println("用户不存在");
                                                break;
                                            }

                                            System.out.println("当前信息：");
                                            System.out.println("用户名：" + Upm.getName());
                                            System.out.println("手机号：" + Upm.getPhone());

                                            System.out.println("请输入新的用户名");
                                            String newMname = scanner.next();
                                            System.out.println("请输入新的手机号");
                                            String newpMhone = scanner.next();
                                            System.out.println("请输入新的密码");
                                            String newMpwd = scanner.next();

                                            Upm.setName(newMname);
                                            Upm.setPhone(newpMhone);
                                            Upm.setPassword(newMpwd);

                                            int c = managerService.updateManager(Upm);
                                            if (c > 0) {
                                                System.out.println("修改成功");
                                            } else {
                                                System.out.println("修改失败");
                                            }
                                            break;
                                        case 4:
                                            System.out.println("请输入要删除的管理员姓名：");
                                            String Dname = scanner.next();
                                            int e = managerService.deleteManager(Dname);
                                            if (d > 0) {
                                                System.out.println("删除成功");
                                            } else {
                                                System.out.println("删除失败");
                                            }
                                            break;
                                        case 5:
                                            System.out.println("已退出管理员用户系统!");
                                            flag = false;
                                            break;


                                    }

                                }
                                break;
                                case 2:
                                    boolean productMenuFlag = true;
                                    while (productMenuFlag) {
                                        System.out.println("=== 商品信息管理系统 ===");
                                        System.out.println("1. 查看所有商品");
                                        System.out.println("2. 添加商品");
                                        System.out.println("3. 删除商品");
                                        System.out.println("4. 修改商品信息");
                                        System.out.println("5. 退出商品信息系统");

                                        int productChoice = scanner.nextInt();

                                        switch (productChoice) {
                                            case 1:
                                                // 查看所有商品
                                                List<Product> productList = productService.findAllProduct();
                                                System.out.print("商品序号" + "\t" + "\t");
                                                System.out.print("商品名称" + "\t" + "\t");
                                                System.out.print("商品库存" + "\t" + "\t");
                                                System.out.print("商品种类" + "\t" + "\t");
                                                System.out.println("商品价格");

                                                for (int k = 0; k < productList.size(); k++) {
                                                    Product p = productList.get(k);
                                                    System.out.print((k + 1) + " \t\t\t");
                                                    System.out.print(p.getProductName() + " \t\t\t");
                                                    System.out.print(p.getStock() + " \t\t");
                                                    System.out.print(p.getCategory() + "\t\t\t");
                                                    System.out.println(p.getPrice());
                                                }
                                                break;

                                            case 2:
                                                // 添加商品
                                                Product newProduct = new Product();
                                                System.out.println("请输入商品名称:");
                                                newProduct.setProductName(scanner.next());
                                                System.out.println("请输入商品库存:");
                                                newProduct.setStock(scanner.nextInt());
                                                System.out.println("请输入商品种类:");
                                                newProduct.setCategory(scanner.next());
                                                System.out.println("请输入商品价格:");
                                                newProduct.setPrice(scanner.nextDouble());
                                                System.out.println("请输入商品描述:");
                                                newProduct.setDescription(scanner.next());

                                                if (productService.insertProduct(newProduct) > 0) {
                                                    System.out.println("商品添加成功");
                                                } else {
                                                    System.out.println("商品添加失败");
                                                }
                                                break;

                                            case 3:
                                                // 删除商品
                                                System.out.println("请输入要删除的商品名称:");
                                                String delName = scanner.next();
                                                if (productService.deleteProduct(delName) > 0) {
                                                    System.out.println("商品删除成功");
                                                } else {
                                                    System.out.println("商品删除失败或不存在");
                                                }
                                                break;

                                            case 4:
                                                // 修改商品信息
                                                System.out.println("请输入要修改的商品名称:");
                                                String oldName = scanner.next();
                                                Product productToUpdate = productService.findByName(oldName);
                                                if (productToUpdate == null) {
                                                    System.out.println("未找到该商品");
                                                    break;
                                                }

                                                System.out.println("当前商品信息:");
                                                System.out.println("名称: " + productToUpdate.getProductName());
                                                System.out.println("库存: " + productToUpdate.getStock());
                                                System.out.println("种类: " + productToUpdate.getCategory());
                                                System.out.println("价格: " + productToUpdate.getPrice());
                                                System.out.println("描述: " + productToUpdate.getDescription());

                                                System.out.println("请输入新的商品名称:");
                                                productToUpdate.setProductName(scanner.next());
                                                System.out.println("请输入新的库存数量:");
                                                productToUpdate.setStock(scanner.nextInt());
                                                System.out.println("请输入新的商品种类:");
                                                productToUpdate.setCategory(scanner.next());
                                                System.out.println("请输入新的价格:");
                                                productToUpdate.setPrice(scanner.nextDouble());
                                                System.out.println("请输入新的描述:");
                                                productToUpdate.setDescription(scanner.next());

                                                if (productService.updateProduct(productToUpdate) > 0) {
                                                    System.out.println("商品信息修改成功");
                                                } else {
                                                    System.out.println("商品信息修改失败");
                                                }
                                                break;

                                            case 5:
                                                productMenuFlag = false;
                                                break;

                                            default:
                                                System.out.println("无效选项，请重新选择");
                                                break;
                                        }
                                    }
                            case 3:
                                boolean orderMenuFlag = true;
                                while (orderMenuFlag) {
                                    System.out.println("=== 订单信息管理系统 ===");
                                    System.out.println("1. 查看所有订单");
                                    System.out.println("2. 按学生姓名删除订单");
                                    System.out.println("3. 退出订单信息系统");

                                    int orderChoice = scanner.nextInt();

                                    switch (orderChoice) {
                                        case 1:
                                            // 查看所有订单
                                            List<Order> orderList = orderService.findAllOrders();
                                            System.out.print("订单编号" + "\t" + "\t");
                                            System.out.print("学生ID" + "\t" + "\t");
                                            System.out.print("商品ID" + "\t" + "\t");
                                            System.out.print("数量" + "\t" + "\t");
                                            System.out.println("价格");

                                            Order[] orders = new Order[0];
                                            for (Order order : orders) {
                                                System.out.print(order.getOrderId() + " \t\t\t");
                                                System.out.print(order.getStudentId() + " \t\t\t");
                                                System.out.print(order.getProductId() + " \t\t");
                                                System.out.print(order.getNumber() + "\t\t\t");
                                                System.out.println(order.getPrice());
                                            }
                                            break;

                                        case 2:
                                            // 删除订单
                                            System.out.println("请输入要删除订单的学生姓名:");
                                            String studentName = scanner.next();
                                            if (orderService.deleteOrderByStudentName(studentName) > 0) {
                                                System.out.println("订单删除成功");
                                            } else {
                                                System.out.println("未找到该学生的订单或删除失败");
                                            }
                                            break;

                                        case 3:
                                            orderMenuFlag = false;
                                            break;

                                        default:
                                            System.out.println("无效选项，请重新选择");
                                            break;
                                    }
                                }

                                    break;
                            case 4:
                                System.out.println("退出管理员系统");
                                flag = false;
                                break;
                        }

                    }
            }
        }
    }
}